<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>主题贴删除工具</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link rel="stylesheet" type="text/css" href="style.css">
		<link rel="stylesheet" type="text/css" href="https://cdn.staticfile.org/Buttons/2.0.0/css/buttons.min.css">
	</head>
	<body>
		<script src="https://cdn.staticfile.org/jquery/1.12.4/jquery.min.js"></script>
		<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
		<h1>贴吧主题贴删除工具</h1>
		<p>BDUSS（<a href="faq.html#bduss">如何获取？</a>）</p>
		<input type="text" id="bduss" placeholder="输入获取到的BDUSS">
		<script>
			function handle(res) {
				$('#stat').append('<p style="color:darkred">致命错误：' + res["status"] + ' (' + res["statusText"] + ')</p>');
				disable();
			}
			function disable() {
				$("#op1").attr("disabled", $("#op1").attr("disabled") ? false : true);
				$("#op2").attr("disabled", $("#op2").attr("disabled") ? false : true);
			}
			async function fetch_post(url, dat, hd) {
				return fetch(url, {
					method: 'POST',
					headers: { 'Content-Type': 'application/json' },
					body: JSON.stringify(dat)
				}).then(async r => {
					if (r.ok) return r.text();
					else if (hd != true) handle(r);
				});
			}
			function s_bduss() {
				$.cookie('bduss', $('#bduss').val(), { expires: 7 });
				if (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) fetch_post('/api/cookie', {'bduss': $('#bduss').val()}, true)
				else $.cookie('bduss', $('#bduss').val(), { expires: 365 });
			}
			if ($.cookie('bduss') !== undefined) {
				$('#bduss').val($.cookie('bduss'));
				s_bduss();
			}
		</script>
		<button type="button" onclick="s_bduss();" class="button button-glow button-border button-rounded button-primary button-width-small button-small">保存</button>
		<p>贴子链接（多个链接请用空格隔开）</p>
		<input type="text" id="link" placeholder="贴子链接">
		<p>删贴理由（必填）</p>
		<a class="sel_mask"> <span>选择删贴理由</span>
			<select class="sel" id="rs1">
				<option value="0">空理由</option>
				<option value="1">恶意刷屏</option>
				<option value="2">不友善言论</option>
				<option value="3">色情低俗</option>
				<option value="4">违法信息</option>
				<option value="5">散布广告</option>
				<option value="6">违反自设吧规</option>
			</select>
		</a>
		<p>封禁理由（可不填）</p>
		<input type="text" id="rs2" placeholder="封禁理由">
		<p>封禁时长（吧主可封1~10天，其他吧务仅可封1天）</p>
		<a class="sel_mask"> <span>选择封禁时长</span>
			<select class="sel" id="day">
				<option selected="selected" value="0">不封禁</option>
				<option value="1">封1天</option>
				<option value="2">封2天</option>
				<option value="3">封3天</option>
				<option value="4">封4天</option>
				<option value="5">封5天</option>
				<option value="6">封6天</option>
				<option value="7">封7天</option>
				<option value="8">封8天</option>
				<option value="9">封9天</option>
				<option value="10">封10天</option>
			</select>
		</a>
		<p>黑名单（默认不加入）</p>
		<a class="sel_mask"> <span>是否加入黑名单</span>
			<select class="sel" id="bl">
				<option selected="selected" value="0">不加入黑名单</option>
				<option value="1">加入黑名单（需用户关注本吧）</option>
			</select>
		</a>
		<script>
			async function del(tp) {
				if (typeof tp !== 'boolean') return;
				if ($.cookie('bduss') === undefined) {
					alert('BDUSS未保存');
					return;
				}
				lnk = $('#link').val().replace(/(^\s*)|(\s*$)/g, "").split(' ');
				for (var i = 0, len = lnk.length; i < len; i++) {
					if (lnk[i] == '') {
						lnk.splice(i, 1);
						i--;
						len--;
						continue;
					}
					lnk[i] = lnk[i].substring(0, (lnk[i].indexOf('?') > 0) ? lnk[i].indexOf('?') : lnk[i].length);
				}
				if (lnk[0] == "" || lnk.length < 1) return;
				disable();
				fetch_post("/api/tbs", {"BDUSS": $.cookie('bduss')}).then(async function(dat) {
					if (JSON.parse(dat)["err"] == 1) {
						$('#stat').html('<p style="color:red">tbs获取失败</p><p>错误信息: ' + dat + '</p>');
					}
					else {
						var tbs = JSON.parse(dat)["tbs"];
						$('#stat').html('<p style="color:green">tbs获取成功</p>');
						for (var i = 0; i < lnk.length; i++) {
							$('#stat').append('<p>正在处理第' + (i+1).toString() + '/' + lnk.length.toString() + '个贴子</p>');
							if (lnk[i].indexOf('tieba.baidu.com/p/') >= 0) {
								await real_del(lnk[i], tbs, tp, $.cookie('bduss'), $('#rs1').val(), $('#day').val(), $('#rs2').val(), $('#bl').val());
							}
							else $('#stat').append('<p style="color:red">链接格式错误，处理失败</p>');
						}
					}
					disable();
				});
			}
			async function real_del(lnk, tbs, tp, bd, rs, day, txt, bl) {
				var info;
				await fetch_post("/api/detail", {"link": lnk}).then(async function(dat) {
					info = JSON.parse(dat);
					if (info["err"] == 1) {
						$('#stat').append('<p style="color:red">贴子数据获取失败</p><p>错误信息: ' + dat + '</p>');
					}
					else {
						if (info["err"] == 2) $('#stat').append('<p><span style="color:green">贴子数据获取成功</span>，<span style="color:red">用户数据获取失败</span></p><p>错误信息: ' + dat + '</p>');
						else $('#stat').append('<p style="color:green">贴子数据获取成功</p>');
						await fetch_post("/api/del", {
							"tid": lnk.substring(lnk.indexOf('p/') + 2, lnk.length),
							"tbs": tbs,
							"fid": info["fid"],
							"mask": Number(!tp),
							"uss": bd,
							"reason": rs
						}).then(async function(dat) {
							if (JSON.parse(dat)["err"] == 1)
								$('#stat').append('<p style="color:red">贴子删除失败</p><p>错误信息: ' + dat + '</p>');
							else $('#stat').append('<p style="color:green">贴子删除成功</p>');
							if (info["err"] == 0) {
								await block(info, tbs, info["fid"], day, txt, info["id"], info["kw"], bl, bd);
							}
						});
					}
				});
			}
			async function block(info, tbs, fid, d, rs, uid, wd, bl, bd) {
				if (Number(d) > 0)
					await fetch_post("/api/ban", {
						"un": info["un"],
						"nk": info["nk"],
						"pt": info["pt"],
						"tbs": tbs,
						"fid": fid,
						"uss": bd,
						"tm": d,
						"reason": rs
					}).then(function(dat) {
						if (JSON.parse(dat)["err"] == 1) {
							$('#stat').append('<p style="color:red">封禁失败</p><p>错误信息: ' + dat + '</p>');
						} else {
							$('#stat').append('<p style="color:green">封禁成功</p>');
						}
					});
				if (bl == "1")
					await fetch_post("/api/black", {
						"uid": uid,
						"bar": wd,
						"tbs": tbs,
						"uss": bd
					}).then(function(dat) {
						if (JSON.parse(dat)["err"] == 1) {
							$('#stat').append('<p style="color:red">黑名单加入失败</p><p>错误信息: ' + dat + '</p>');
						} else {
							$('#stat').append('<p style="color:green">黑名单加入成功</p>');
						}
					});
			}
		</script>
		<br /><br /><button type="button" id="op1" onclick="del(true);" class="button button-glow button-rounded button-raised button-primary">删除</button>
		<br /><br /><button type="button" id="op2" onclick="del(false);" class="button button-glow button-rounded button-raised button-primary">屏蔽</button>
		<div id="stat"></div>
	</body>
</html>
